package com.bootdo.common.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.bootdo.shop.dao.WxShopCatDao;
import com.bootdo.shop.domain.WxShopCatDO;
import com.bootdo.wx.service.MemberTokenService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 同步微信自定义组件类型
 */
@Component
public class WxThirdCatJob implements Job {
    final  String catUrl = "https://api.weixin.qq.com/shop/cat/get?access_token=";

    @Autowired
    MemberTokenService memberTokenService;
    @Autowired
    WxShopCatDao wxShopCatDao;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String token = memberTokenService.getToken();
        TimeInterval timer = DateUtil.timer();
        JSONObject jsonObject1 = JSONUtil.parseObj(MapUtil.newHashMap());
        String s1 = jsonObject1.toJSONString(8);
        String s = HttpUtil.post(catUrl + token, s1,  1000);
        JSONObject jsonObject = JSONUtil.parseObj(s);
        if(jsonObject.getInt("errcode")==0){
            JSONArray thirdCatList = jsonObject.getJSONArray("third_cat_list");
            Iterator<Object> iterator = thirdCatList.iterator();
            if(thirdCatList.size()>0){
                List<WxShopCatDO> list = new ArrayList<>();
                while(iterator.hasNext()) {
                    JSONObject catObject = (JSONObject)iterator.next();
                    WxShopCatDO wxShopCatDO = new WxShopCatDO();
                    wxShopCatDO.setId(IdUtil.simpleUUID());
                    wxShopCatDO.setCaAt(new Date());
                    wxShopCatDO.setDelFlag(0);
                    wxShopCatDO.setFirstCatId(catObject.getLong("first_cat_id"));
                    wxShopCatDO.setFirstCatName(catObject.getStr("first_cat_name"));
                    wxShopCatDO.setSecondCatId(catObject.getLong("second_cat_id"));
                    wxShopCatDO.setSecondCatName(catObject.getStr("second_cat_name"));
                    wxShopCatDO.setThirdCatId(catObject.getLong("third_cat_id"));
                    wxShopCatDO.setThirdCatName(catObject.getStr("third_cat_name"));
                    wxShopCatDO.setQualification(catObject.getStr("qualification"));
                    wxShopCatDO.setQualificationType(catObject.getInt("qualification_type"));
                    wxShopCatDO.setProductQualification(catObject.getStr("product_qualification"));
                    wxShopCatDO.setProductQualificationType(catObject.getInt("product_qualification_type"));
                    list.add(wxShopCatDO);
                }
                int save = wxShopCatDao.batchInsert(list);
            }



        }

        System.out.println(jsonObject);
        timer.intervalMinute();//花费分钟数
    }
}
